<?php

namespace app\controller;

use app\BaseController;
use think\Request;
use app\service\SaleOutlayService;
use Exception;
use think\facade\Db;
use app\model\StockModel;

class SaleOutlay extends BaseController
{    
    protected $middleware = [
        \app\middleware\LoginCheck::class
    ];
    protected $request;
    protected $saleoutlayService;
    public function __construct(Request $request,SaleOutlayService $saleoutlayService)
    {   
        $this->request = $request;
        $this->saleoutlayService = $saleoutlayService;
    }    

    public function list()
    {       
        try {          
            $params = $this->request->param();             
            $data = $this->saleoutlayService->list($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }   
    
    public function listInfo()
    {       
        try {     
            $params = $this->request->param();
            $data = $this->saleoutlayService->listInfo($params['id']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function info()
    {       
        try {     
            $params = $this->request->param();       
            $data = $this->saleoutlayService->info($params['id']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    } 

    public function infoByPid()
    {       
        try {     
            $params = $this->request->param();
            $data = $this->saleoutlayService->infoByPid($params['type'],$params['pid']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    } 
    
    public function save()
    {       
        try {    
            $params = $this->request->param(); 
            if(empty($params['operator_id'])){
                $params['operator_id'] = $this->request->userInfo->id;
            }            
            $data = $this->saleoutlayService->save($params);
            if(empty($params['is_review'])){            
                $stock = Db::name('stock')->where(['id'=>$params['stock_id']])->find();  
                $data = [
                    "type" => $stock['type'],
                    "buy_id"=>$stock['buy_id'],
                    "sid"=>$stock['id'],
                    "start_date"=>$params['warehousing_date'],
                    "num"=>$params['num'],
                    "payment_no"=>$stock['payment_no'],
                    "warehouses_id"=>$stock['warehouses_id'],
                    "product_no"=>$stock['product_no'],
                    "cost_price"=>$stock['cost_price'],
                    "category_id"=>$stock['category_id'],
                    "product_name" =>$stock['product_name'],
                    "sn" =>"CK-" . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
                    "operator_id"=>$params['operator_id'],
                    "remark"=> $params['remark'],
                    "stat"=>1,    
                    "delivery_no"=>$params['delivery_no'],       
                    "create_time"=>time(),
                    "update_time"=>time(),
                    "order_id" => $data['id']
                ];
                $data = StockModel::insert($data); 
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function delete()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->saleoutlayService->delete($params);
            StockModel::destroy(['order_id'=>$params['id']]);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function csv()
    {       
        try {          
            $params = $this->request->param();             
            $data = $this->saleoutlayService->csv($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }
}
